<?php
class Testimonial extends CI_Controller{
	private $per_page_index = 1;
	
	public function __construct(){
		parent::__construct();
		$this->load->model('model_testimonial');
	}
	
	public function index(){
		$pesansukses = "";
		$data['active_testimonial'] = 'y';
		$data['title'] = TITLE.' - Testimonial';
		$data['javascript'][] = "ajax_paging.js";
		
		if($this->input->post("btnSubmit")){
			$hasil = $this->_saveTestimonial();
			if($hasil) $pesansukses = "Thank you for sending your testimonial. We will review it soon.";
			$data['pesansukses'] = $pesansukses;
		}
		
		$params['is_shown'] = 'Y';
		$params['order_by'] = "entry_date desc, testimonial_id desc";
		$count = count($this->model_testimonial->getTestimonial($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'testimonial/load';
		$config['uri_segment'] = 3;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = 0;
		$data['testimonial'] = $this->model_testimonial->getTestimonial($params);
		$data['per_page_index'] = $this->per_page_index;
		
		$this->load->view("testimonial",$data);
	}
	
	function load($offset=0){
		$params['is_shown'] = 'Y';
		$params['order_by'] = "entry_date desc, testimonial_id desc";
		$count = count($this->model_testimonial->getTestimonial($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'testimonial/load';
		$config['uri_segment'] = 3;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = $offset;
		$data['testimonial'] = $this->model_testimonial->getTestimonial($params);
		$data['per_page_index'] = $this->per_page_index;

		$this->load->view("content_load/testimonial_load",$data);
	}
	
	function _saveTestimonial(){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'testimonial_desc',
                     'label'   => 'Testimonial Content',
                     'rules'   => 'trim|required|xss_clean'
                  )
				);
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_testimonial->save($this->input->post("testimonial_desc",true));
			return true;
		}
	}
	

}